package com.xyht.sca_s.student_manage_system.modules.stampManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.SmsStampManagement;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.dto.SmsStampAddDto;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.dto.SmsStampDeletedDto;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.dto.SmsStampUpdateDto;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampDetailVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampListVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampUserVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.mapper.SmsStampManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.service.SmsStampManagementService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 印章管理 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-31
 */
@Service
public class SmsStampManagementServiceImpl extends ServiceImpl<SmsStampManagementMapper, SmsStampManagement> implements SmsStampManagementService {

    @Resource
    private SmsStampManagementMapper smsStampManagementMapper;
    @Resource
    private CacheUtil cacheUtil;


    @Override
    public ResponseResult addStamp(String userId, SmsStampAddDto smsStampAddDto) {

        //判断印章名字不可重复
        List<SmsStampManagement> smsStampManagements = smsStampManagementMapper.selectList(new QueryWrapper<SmsStampManagement>()
                .lambda()
                .eq(SmsStampManagement::getStampName, smsStampAddDto.getStampName()));
        if(smsStampManagements != null && smsStampManagements.size() > 0){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NAME_DUPLICATION);
        }
        SmsStampManagement addStamp = new SmsStampManagement();
        BeanUtils.copyProperties(smsStampAddDto,addStamp);
        addStamp.setStampAddUserId(userId);
        int result = smsStampManagementMapper.insert(addStamp);
        if(result == 0){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_ADD_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedStamp(SmsStampDeletedDto smsStampDeletedDto,String userId) {
        if(isNullOrEmpty(smsStampDeletedDto.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsStampManagement smsStampManagement = smsStampManagementMapper.selectById(smsStampDeletedDto.getId());
        if(smsStampManagement == null){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NOT_EXIST);
        }

        int result = smsStampManagementMapper.delete(new QueryWrapper<SmsStampManagement>()
                .lambda()
                .eq(SmsStampManagement::getId, smsStampDeletedDto.getId())
                .eq(SmsStampManagement::getStampAddUserId, userId));

        if(result == 0){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_DELETE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateStampInfo(String userId, SmsStampUpdateDto smsStampUpdateDto) {
        if(isNullOrEmpty(smsStampUpdateDto.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsStampManagement smsStampManagement = smsStampManagementMapper.selectById(smsStampUpdateDto.getId());
        if(smsStampManagement == null){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NOT_EXIST);
        }
        //判断印章名字不可重复
        SmsStampManagement updateSmsStampManagement = smsStampManagementMapper.selectOne(new QueryWrapper<SmsStampManagement>()
                .lambda()
                .eq(SmsStampManagement::getStampName, smsStampUpdateDto.getStampName())
                .ne(SmsStampManagement::getId,smsStampUpdateDto.getId()));
        if(updateSmsStampManagement != null ){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NAME_DUPLICATION);
        }

        BeanUtils.copyProperties(smsStampUpdateDto,smsStampManagement);

        int result = smsStampManagementMapper.updateById(smsStampManagement);
        if(result == 0){
            return CommonResult.failed(CommonCodeEnum.UPDATE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStampDetail(String id,String userId) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsStampManagement smsStampManagement = smsStampManagementMapper.selectOne(new QueryWrapper<SmsStampManagement>()
                .lambda()
                .eq(SmsStampManagement::getId, id)
                .eq(SmsStampManagement::getStampAddUserId, userId));
        if(smsStampManagement == null || isNullOrEmpty(smsStampManagement.getId())){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NOT_EXIST);
        }
        SmsStampDetailVo smsStampDetailVo = new SmsStampDetailVo();
        BeanUtils.copyProperties(smsStampManagement,smsStampDetailVo);
        SmsUser userInfo = cacheUtil.getUserInfo(smsStampManagement.getStampAddUserId());
        SmsStampUserVo smsStampUserVo = new SmsStampUserVo();
        if(userInfo != null){
            BeanUtils.copyProperties(userInfo,smsStampUserVo);
        }
        smsStampDetailVo.setStampAddUserInfo(smsStampUserVo);

        return CommonResult.success(smsStampDetailVo);
    }

    @Override
    public ResponseResult getStampList(String userId,Integer pageSize,Integer pageNum,String stampName,Integer stampType) {
        Page<SmsStampManagement> page = new Page<>(pageNum,pageSize);
        List<SmsStampListVo> stampVoList = new ArrayList<>();
        QueryWrapper<SmsStampManagement> stampManagementQueryWrapper = new QueryWrapper<>();
        stampManagementQueryWrapper.lambda()
                    .eq(SmsStampManagement::getStampAddUserId, userId)
                    .orderByDesc(SmsStampManagement::getCreateTime);
        if(!isNullOrEmpty(stampName)){
            stampManagementQueryWrapper.lambda().like(SmsStampManagement::getStampName,stampName);
        }
        if(!isNullOrEmpty(stampType)){
            stampManagementQueryWrapper.lambda().eq(SmsStampManagement::getStampType,stampType);
        }
        smsStampManagementMapper.selectPage(page,stampManagementQueryWrapper);
        stampVoList = page.getRecords()
                .stream()
                .map(smsStamp ->{
                    SmsStampListVo smsStampVo = new SmsStampListVo();
                    BeanUtils.copyProperties(smsStamp,smsStampVo);
                    SmsStampUserVo smsStampUserVo = new SmsStampUserVo();
                    SmsUser userInfo = cacheUtil.getUserInfo(userId);
                    if(userInfo != null){
                        BeanUtils.copyProperties(userInfo,smsStampUserVo);
                    }
                    smsStampVo.setAddStampUserInfo(smsStampUserVo);
                    return smsStampVo;
                })
        .collect(Collectors.toList());
        int total = (int)page.getTotal();
        return CommonResult.success(stampVoList,total);
    }


}
